package com.example.restful01.controller;

import com.example.restful01.model.Book;
import com.example.restful01.model.Comments;
import com.example.restful01.service.BookService;
import com.example.restful01.service.CommentsService;
import com.example.restful01.util.Result;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api") // 与前端axios的baseURL对应
@CrossOrigin // 跨域注解，也可以使用上面的全局配置
public class CommentsController {

    @Autowired
    private CommentsService commentsService;

    // 评论列表接口
    @GetMapping("/comments")
    public Result initCommentsList() {
        return Result.success(commentsService.list());
    }

    // 书籍评论接口
    @DeleteMapping("/deletecomment")
    public Result addBooks(@RequestParam Integer commentId) {
        if (commentsService.removeById(commentId)) {
            return Result.success("删除评论成功！");
        } else return Result.error("删除评论失败！");
    }

    @PutMapping("/addpraise")
    public Result addPraise(@RequestParam Integer commentId) {
        int i = commentsService.addPraise(commentId);
        if (i > 0) {
            return Result.success("点赞成功！");
        } else {
            return Result.error("点赞失败！");
        }
    }

    @PostMapping("/addcomment")
    public Result addComment(@RequestBody Comments comments) {
        int i = commentsService.addComment(comments);
        if (i > 0) {
            return Result.success("添加评论成功！");
        } else {
            return Result.error("添加评论失败！");
        }
    }
    @PostMapping("/reportcomment")
    public Result reportComment(@RequestBody Comments comments) {
        if (commentsService.reportComment(comments)) {
            return Result.success("举报成功！");
        } else {
            return Result.error("举报失败！");
        }
    }
}